/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "Prefab.h"
#include "TinyXML.h"

#include "../Managers/ContentLibrary.h"

namespace Hogshead
{
	namespace Core
	{
		ContentCPP(Hogshead::Core::Prefab, Prefab)

		Prefab::Prefab(void)
		{
			_root = NULL;
			_ref_count = 0;
		}


		Prefab::~Prefab(void)
		{
		}

		Prefab* Prefab::getPrefab(const String& in_path)
		{
			Prefab* requested = NULL;
			//first check and see if its loaded, if it is return it, w/o ref counter increment
			if( _path_to_prefabs.contains( in_path )  )
			{
				//we know its in there at this point
				_path_to_prefabs.tryGetValue( in_path, requested );
			}
			//otherwise load it and return it
			//loading it will set its ref counter to 1
			else
			{
				//load it
				requested = (Prefab*) ContentLibrary::loadAndParseContentXML( in_path );
				//add it to the path hash table
				//_path_to_prefabs.add( requested->_path, requested );
				_path_to_prefabs.add( in_path, requested );
			}
			return requested;
		}

		void Prefab::finalize()
		{
			IGameObjectTreeHolder::finalize();

			if(_root)
				Factory::release( _root );
		}

		void Prefab::explicitLoad()
		{
			error("You cannot load a Prefab explicitely, XML is required");
		}

		void Prefab::readXML(const String& in_xml)
		{
			//parse the String for what we need
			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			String root_tag(root_element->Value());

			if(root_tag.equals("Content") && String::equals(root_element->Attribute("type"), "Prefab"))
			{
				//get the standard stuff
				this->_key = String(root_element->Attribute("key"));
				this->_path = String(root_element->Attribute("path"));

				//used to get the absolute path to it 
				this->_path = ContentLibrary::getCompleteContentString( this->_path );


				TiXmlNode* child_node = NULL;
				TiXmlElement* child_element;

				if(child_node = root_element->IterateChildren(child_node))
				{
					child_element = child_node->ToElement();
					if(!child_element)
						return;
					
					String tag(child_element->Value());
					if(tag.equals("GameObject"))
					{
						String go_xml(*child_element);
						_root = Factory::acquire<GameObject>();
						_root->readXML(go_xml, this);
						_root->setOwner(this);

					}
				}
			}

		}

	}
}